package com.kob.kobbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kob.kobbackend.mapper.BotMapper;
import com.kob.kobbackend.pojo.Bot;
import com.kob.kobbackend.pojo.User;
import com.kob.kobbackend.service.user.bot.UserBotService;
import com.kob.kobbackend.utils.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserBotServiceImpl implements UserBotService {
    @Autowired
    private BotMapper botMapper;

    @Override
    public Map<String, String> add(Map<String, String> data) {
        UsernamePasswordAuthenticationToken token =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) token.getPrincipal();
        User user = loginUser.getUser();

        String title = data.get("title");
        String description = data.get("description");
        String content = data.get("content");

        Map<String, String> map = new HashMap<>();

        if(title == null || title.isEmpty()) {
            map.put("result","标题不能为空");
            return map;
        }

        if(title.length()>100){
            map.put("result","标题不能大于100");
            return map;
        }

        if(description == null || description.isEmpty()) {
            description="这个用户很懒，什么也没留下";
        }

        if(description.length()>300){
            map.put("result","Bot描述的长度不能大于300");
            return map;
        }

        if(content == null || content.isEmpty()) {
            map.put("result","代码不能为空");
            return map;
        }

        if(content.length()>10000){
            map.put("result","代码长度不能超过一万");
            return map;
        }

        QueryWrapper<Bot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getId());
        if(botMapper.selectCount(queryWrapper)>=10){
            map.put("result","每个用户只能创建十个Bot");
            return map;
        }

        Date now = new Date();
        Bot bot = new Bot(null,user.getId(),title,description,content,now,now);

        botMapper.insert(bot);
        map.put("result","success");

        return map;
    }

    @Override
    public Map<String, String> remove(Map<String, String> data) {
        UsernamePasswordAuthenticationToken token =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) token.getPrincipal();
        User user = loginUser.getUser();

        int botId = Integer.parseInt(data.get("bot_id"));
        Bot bot = botMapper.selectById(botId);
        Map<String,String> map = new HashMap<String,String>();

        if(bot ==null){
            map.put("result", "Bot不存在或已被删除");
            return map;
        }

        if(!bot.getUserId().equals(user.getId())){
            map.put("result", "没有权限删除该Bot");
            return map;
        }

        botMapper.deleteById(botId);
        map.put("result", "success");

        return map;
    }

    @Override
    public Map<String, String> update(Map<String, String> data) {
        UsernamePasswordAuthenticationToken token =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) token.getPrincipal();
        User user = loginUser.getUser();

        int bot_id = Integer.parseInt(data.get("bot_id"));

        String title = data.get("title");
        String description = data.get("description");
        String content = data.get("content");

        Map<String, String> map = new HashMap<>();

        if(title == null || title.isEmpty()) {
            map.put("result","标题不能为空");
            return map;
        }

        if(title.length()>100){
            map.put("result","标题不能大于100");
            return map;
        }

        if(description == null || description.isEmpty()) {
            description="这个用户很懒，什么也没留下";
        }

        if(description.length()>300){
            map.put("result","Bot描述的长度不能大于300");
            return map;
        }

        if(content == null || content.isEmpty()) {
            map.put("result","代码不能为空");
            return map;
        }

        if(content.length()>10000){
            map.put("result","代码长度不能超过一万");
            return map;
        }

        Bot bot = botMapper.selectById(bot_id);
        if(bot == null){
            map.put("result", "Bot不存在或已被删除");
            return map;
        }

        if(!bot.getUserId().equals(user.getId())){
            map.put("result", "没有权限修改该Bot");
            return map;
        }

        Bot new_bot = new Bot(bot.getId(),user.getId(),title,description,content,bot.getCreatetime(),new Date());

        botMapper.updateById(new_bot);
        map.put("result","success");
        return map;
    }

    @Override
    public List<Bot> getList() {
        UsernamePasswordAuthenticationToken token =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) token.getPrincipal();
        User user = loginUser.getUser();

        QueryWrapper<Bot> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());

        return botMapper.selectList(queryWrapper);
    }
}
